/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$(".btn-ed").click(function () {
    cleandata();
    var id = $(this).data("id");
    $("#prjecttypeid").val(id);
    var datastr = "cont=1&projecttypeid=" + id;
    $.ajax({
        url: "/../../controller/project/projecttypecontroller.php",
        type: "POST",
        data: datastr,
        success: function (e) {
            var result = eval("(" + e + ")");
            if (result.code == 0) {
                $("#projecttype_name").val(result.data.name);
                $("#projecttype_remark").val(result.data.remark);
                $("#projecttype_seq").val(result.data.seq);
                if (result.data.isformula == 1) {
                    $("#formula_y").prop("checked", true);
                    $("#formula_n").prop("checked", false);
                } else {
                    $("#formula_y").prop("checked", false);
                    $("#formula_n").prop("checked", true);
                }
                $('.i-checks').iCheck({
                    checkboxClass: 'icheckbox_square-green',
                    radioClass: 'iradio_square-green',
                });
            }
        }
    });
});

$(".btn-dt").click(function () {
    var id = $(this).data("id");
    var datastr = "cont=2&projecttypeid=" + id;
    layer.confirm('是否刪除？', {
        btn: ['确认', '取消'] //可以无限个按钮
    }, function (index, layero) {
        $.ajax({
            url: "/../../controller/project/projecttypecontroller.php",
            type: "POST",
            data: datastr,
            success: function (e) {
                var result = eval("(" + e + ")");
                if (result.code == 0) {
                    $("#userlist" + id).css("display", "none");
                    layer.msg('操作成功', {
                        time: 2000, //20s后自动关闭
                        btn: ['OK']
                    });
                } else {
                    layer.msg('操作失败,请刷新重试', {
                        time: 2000, //20s后自动关闭
                        btn: ['OK']
                    });
                }
            }
        });
    }
    );
});

function cleandata() {
    $("#prjecttypeid").val("");
    $("#projecttype_name").val("");
    $("#projecttype_remark").val("");
    $("#projecttype_seq").val("");
    $("#formula_y").prop("checked", false);
    $("#formula_n").prop("checked", true);
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green',
    });
    console.log("clean successful!");
}

function deleteall(ids) {
    var datastr = "cont=3&ids=" + ids;
    layer.confirm('是否刪除？', {
        btn: ['确认', '取消'] //可以无限个按钮
    }, function (index, layero) {
        $.ajax({
            url: "/../../controller/project/projecttypecontroller.php",
            type: "POST",
            data: datastr,
            success: function (e) {
                var result = eval("(" + e + ")");
                if (result.code == 0) {
                    //直接刷新
                    layer.msg('操作成功', {
                        time: 2000, //20s后自动关闭
                        btn: ['OK']
                    }, function () {
                        window.location.reload();
                    });
                } else {
                    layer.msg('操作失败,请刷新重试', {
                        time: 2000, //20s后自动关闭
                        btn: ['OK']
                    });
                }
            }
        });
    }
    );
}
$("#submitchangetype").click(function () {
    var projecttype_name = $("#projecttype_name").val();
    var projecttype_remark = $("#projecttype_remark").val();
    var projecttype_seq = $("#projecttype_seq").val();
    if (projecttype_name == "" || projecttype_remark == "" || projecttype_seq == "") {
        layer.msg('信息请填写完整', {
            time: 2000, //20s后自动关闭
            btn: ['OK']
        });
        return;
    }

    var isformula = $('input:radio[name="isformula"]:checked').val();
    var isf = 0;
    if (isformula == null) {
        layer.msg('信息请填写完整', {
            time: 2000, //20s后自动关闭
            btn: ['OK']
        });
        return;
    } else {
        if (isformula == 1) {
            isf = 1;
        } else {
            isf = 0;
        }
    }

    var id = $("#prjecttypeid").val();
    var datastr = "";

    if (id == 0) {
        datastr = "cont=4";
        datastr += "&pname=" + projecttype_name;
        datastr += "&premark=" + projecttype_remark;
        datastr += "&pseq=" + projecttype_seq;
        datastr += "&isformula=" + isf;
    } else {
        datastr = "cont=5";
        datastr += "&ptid=" + id;
        datastr += "&pname=" + projecttype_name;
        datastr += "&premark=" + projecttype_remark;
        datastr += "&pseq=" + projecttype_seq;
        datastr += "&isformula=" + isf;
    }
    $.ajax({
        url: "/../../controller/project/projecttypecontroller.php",
        type: "POST",
        data: datastr,
        success: function (e) {
            var result = eval("(" + e + ")");
            layer.msg('操作成功', {
                time: 2000, //20s后自动关闭
                btn: ['OK']
            }, function () {
                window.location.reload();
            });
        }
    });
});
